/*! ******************************************************************************
 *
 * Created on 2022年3月25日
 *
 *
 * Copyright(c) YuZhou Big Data Laboratory CO.,Ltd, 2022.  All rights reserved.
 *
 *******************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ******************************************************************************/

package org.yzbdl.lanius.core.metastore;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.util.ApplicationPathUtil;

import cn.hutool.db.Db;
import cn.hutool.db.ds.DSFactory;
import cn.hutool.db.handler.RsHandler;
import cn.hutool.db.sql.SqlExecutor;
import cn.hutool.setting.Setting;

/**
 * @author hs@yzbdl.ac.cn
 *
 */
public class HutoolDb extends Db {

    /** 数据库配置文件路径 */
    private static final String DB_SETTING_PATH = "db.setting";

    private static final long serialVersionUID = 1L;

    // private static UpperCaseWrapper wrapper = new UpperCaseWrapper();
    /** 使用小写 */
    private static LowerCaseWrapper wrapper = new LowerCaseWrapper();

    private static HutoolDb hutoolDb = null;

    /**
     * @param ds
     */
    public HutoolDb(DataSource ds) {
        super(ds);
    }

    public static HutoolDb useDb() {
        HutoolDb db = useDynamicUrlDb();
        db.setWrapper(wrapper);
        return db;
    }

    /**
     *
     * @return
     */
    public static HutoolDb useDynamicUrlDb() {
        if (null == hutoolDb) {
            Setting setting = new Setting(DB_SETTING_PATH);
            // String dbHome = Const.getUserHomeDirectory();
            String dbHome = ApplicationPathUtil.getPath();
            // String ver = VersionUtil.getLaniusVersionTopTwo();
            String dbUrl = setting.get("url").replace("${" + Const.H2_URL_DB_DIR + "}", dbHome);
            // .replace("${" + Const.H2_URL_PARAMS + "}", ver)
            setting.set("url", dbUrl);
            DataSource ds = DSFactory.create(setting).getDataSource();
            hutoolDb = new HutoolDb(ds);
        }
        return hutoolDb;
    }

    /**
     *
     * @return
     */
    public static HutoolDb useDynamicUrlDb(String ver) {

        Setting setting = new Setting(DB_SETTING_PATH);
        // String ver = VersionUtil.getLaniusVersionTopTwo();
        String dbUrl = setting.get("url").replace("${" + Const.H2_URL_PARAMS + "}", ver);
        setting.set("url", dbUrl);
        DataSource ds = DSFactory.create(setting).getDataSource();
        hutoolDb = new HutoolDb(ds);

        return hutoolDb;
    }

    public static void resetHutooldb() {
        hutoolDb = null;
    }

    public static HutoolDb tempDynamicUrlDb(String dbUrl) {
        Setting setting = new Setting(DB_SETTING_PATH);
        setting.set("url", dbUrl);
        DataSource ds = DSFactory.create(setting).getDataSource();
        hutoolDb = new HutoolDb(ds);

        return hutoolDb;
    }

    /**
     * 查询
     *
     * @param <T>    结果集需要处理的对象类型
     * @param sql    查询语句
     * @param rsh    结果集处理对象
     * @param params 参数
     * @return 结果对象
     * @throws SQLException SQL执行异常
     */
    public <T> T query(String sql, RsHandler<T> rsh, Object... params) throws SQLException {
        sql = wrapper.formatSql(sql);

        Connection conn = null;
        try {
            conn = this.getConnection();
            return SqlExecutor.query(conn, sql, rsh, params);
        } finally {
            this.closeConnection(conn);
        }
    }

}
